package com.sskj.mine.account;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;

import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/03
 */
public class AddAccountActivity extends BaseActivity<AddAccountPresenter> {

    @BindView(R2.id.user_name_edt)
    EditText userNameEdt;
    @BindView(R2.id.user_account_edt)
    EditText userAccountEdt;
    @BindView(R2.id.user_password_edt)
    EditText userPasswordEdt;
    @BindView(R2.id.user_password2_edt)
    EditText userPassword2Edt;
    @BindView(R2.id.submit_btn)
    Button submitBtn;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_add_account;
    }

    @Override
    public AddAccountPresenter getPresenter() {
        return new AddAccountPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_addAccountActivity1_j));
    }

    @Override
    public void initData() {
        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(userNameEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addAccountActivity2_j));
                return;
            }
            if (isEmpty(userAccountEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addAccountActivity3_j));
                return;
            }
            if (userAccountEdt.getText().toString().length() != 8) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addAccountActivity4_j));
                return;
            }
            if (isEmpty(userPasswordEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addAccountActivity5_j));
                return;
            }
            if (isEmpty(userPassword2Edt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addAccountActivity6_j));
                return;
            }

            if (!CheckUtil.isPayPwd(getText(userPasswordEdt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity13_j));
                return;
            }
            if (!getText(userPasswordEdt).equals(getText(userPassword2Edt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity14_j));
                return;
            }
            String account = userAccountEdt.getText().toString().trim();
            String name = userNameEdt.getText().toString().trim();
            String pwd = userPasswordEdt.getText().toString().trim();
            String pwd2 = userPassword2Edt.getText().toString().trim();
            mPresenter.addAccount(account, name, pwd, pwd2);
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddAccountActivity.class);
        context.startActivity(intent);
    }

}
